package Client2;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class Client {
	/**/
	private int    port     = 5555;
	// private String serverIp = "10.10.11.118";
	private String serverIp = "localhost";
	
	public Client() {
		
		try {
			
			Socket socket = new Socket(serverIp, port);
			
			ClientWriteThread clientWriteThread = new ClientWriteThread(socket);
			ClientReadThread clientReadThread = new ClientReadThread(socket);
			clientWriteThread.start();
			clientReadThread.start();
			
		} catch(IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) {
		
		new Client();
	}
	
	class ClientWriteThread extends Thread {
		
		private Socket socket;
		
		public ClientWriteThread(Socket socket) {
			this.socket = socket;
		}
		
		@Override
		public void run() {
			try {
				
				DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
				
				while(true) {
					String message = dataInputStream.readUTF();
					System.out.println("server : " + message);
				}
				
			} catch(IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	class ClientReadThread extends Thread {
		
		private Socket socket;
		
		public ClientReadThread(Socket socket) {
			this.socket = socket;
		}
		
		@Override
		public void run() {
			try {
				
				DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
				Scanner scanner = new Scanner(System.in);
				
				while(true) {
					String message = scanner.nextLine();
					dataOutputStream.writeUTF(message);
				}
				
			} catch(IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
